package com.mairuide._frame.utils.sheethandler.oneKey;

import com.mairuide._frame.utils.ReflectUtils;
import com.mairuide._frame.utils.RegUtil;
import com.mairuide._frame.utils.SpringUtil;
import com.mairuide.bill.utils.DateUtils;
import com.mairuide.exceltitle_dict.enum2.FieldTypeEnum;
import com.mairuide.one_click_search.pojo.DataSource;
import com.mairuide.one_click_search.pojo.DataTemplate;
import com.mairuide.one_click_search.pojo.DataTemplateField;
import com.mairuide.one_click_search.service.DataTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-19 17:51
 */
public class OneKeySheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private  AtomicInteger count = new AtomicInteger(0);
    private Map<String,Object> resultMap = new HashMap<String,Object>();  //结果MAP
    private Map<String,Object> paraMap = new HashMap<String,Object>();  //参数MAP
    private DataSource dataSource;
    private Map<String, DataTemplateField> dataTemplateFieldMap = null;
    private  List<DataSource> data = new ArrayList<DataSource>();
    private DataTemplate dataTemplate;
    private Integer currentRow; //当前行
    @Override
    public void startRow(int i) {
        currentRow=i;
        if(dataTemplate!=null&&dataTemplate.getBeginLine()<i) {
            dataSource = new DataSource();
        }
    }

    @Override
    public void endRow(int i) {
        if (dataSource!=null){
            int successNum = count.incrementAndGet();
            data.add(dataSource);
            resultMap.put("successNum",successNum);
        }
        if (data.size()>=10000){
            doSomething();
            data = new ArrayList<DataSource>();
        }
        dataSource=null;
    }

    @Override
    public void cell(String cellReference, String value, XSSFComment xssfComment) {
        List matcher = RegUtil.getMatcher("([a-zA-Z]+)", cellReference);
        String pix = matcher.get(0).toString();
        if (StringUtils.isBlank(value)) return;
       // System.out.println("当前行--》》》"+currentRow+"。值为：：》》"+value);
        Object object = value;
        if (dataSource!=null && dataTemplateFieldMap!=null){
            DataTemplateField dataTemplateField = dataTemplateFieldMap.get(pix);
            if (dataTemplateField!=null){
                //数据库字段
                String classField = dataTemplateField.getColname();
                try {
                    Class aClass = FieldTypeEnum.getFieldTypeEnumByVal(dataTemplateField.getSjlx());
                    if (aClass.equals(Timestamp.class) ){
                        Date d= DateUtils.getDate(value);
                        if (d==null) return;
                        object = DateUtils.getDateString(d);
                    }else if (aClass.equals(BigDecimal.class) ){
                        object=new BigDecimal(value);
                        object=String.valueOf(object);
                    }else if (aClass.equals(Integer.class) ){
                        object=Integer.valueOf(value);
                        object=String.valueOf(object);
                    }
                    ReflectUtils.setValue(dataSource,DataSource.class,classField,String.class,object);
                } catch (NumberFormatException e) {
                    logger.info("导入报错》》》值为："+object+",字段为："+classField+",第"+currentRow+"行");
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void headerFooter(String text, boolean isHeader, String tagName) {
    }


    public void doSomething(){
        for (int i = 0; i < data.size(); i++) {
            DataSource dataSource = data.get(i);
            dataSource.setTablename(dataTemplate.getTablename());
            dataSource.setUuid(paraMap.get("uuid").toString());
            dataSource.setCreateId((Integer) paraMap.get("platManagerId"));
            dataSource.setCreateTime(new Timestamp(System.currentTimeMillis()));
            dataSource.setIsDelete(0);
        }
        DataTemplateService dataTemplateService = SpringUtil.getBean(DataTemplateService.class);
        dataTemplateService.batchSave(data);
    }

    public Map<String, Object> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, Object> resultMap) {
        this.resultMap = resultMap;
    }


    public AtomicInteger getCount() {
        return count;
    }

    public void setCount(AtomicInteger count) {
        this.count = count;
    }

    public Map<String, Object> getParaMap() {
        return paraMap;
    }

    public void setParaMap(Map<String, Object> paraMap) {
        this.paraMap = paraMap;
    }

    public Map<String, DataTemplateField> getDataTemplateFieldMap() {
        return dataTemplateFieldMap;
    }

    public void setDataTemplateFieldMap(Map<String, DataTemplateField> dataTemplateFieldMap) {
        this.dataTemplateFieldMap = dataTemplateFieldMap;
    }

    public List<DataSource> getData() {
        return data;
    }

    public void setData(List<DataSource> data) {
        this.data = data;
    }

    public DataTemplate getDataTemplate() {
        return dataTemplate;
    }

    public void setDataTemplate(DataTemplate dataTemplate) {
        this.dataTemplate = dataTemplate;
    }
}
